/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DA;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import java.util.Properties;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

/**
 *
 * @author thangvv0907i
 */
public class ConfigFile {
    
    public static final String CONFIG_FILENAME = "configs.propeties";
    public static final String CONFIG_SERVERNAME = "serverName";
    public static final String CONFIG_PORTNUMBER = "portNumber";
    public static final String CONFIG_DATABASENAME = "databaseName";
    public static final String CONFIG_SERVERUSERNAME = "userName";
    public static final String CONFIG_SERVERPASSWORD = "password";

    private static final String PASSKEY = "2601176";
    private static final int count = 20;
    private static final byte[] salt = {
            (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
            (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
        };


    private static boolean logged = false;

    private static Properties p;

    public static boolean isLogged() {
        return logged;
    }

    public static void setLogged(boolean b) {
        logged = b;
    }

    //Method Encrypt Password
    public static String encrypt(String input) throws Exception {
        KeySpec keySpec = new PBEKeySpec(PASSKEY.toCharArray(), salt, count);

        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        Cipher ecipher = Cipher.getInstance(key.getAlgorithm());
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, count);
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        byte[] utf8 = input.getBytes("UTF8");
        byte[] enc = ecipher.doFinal(utf8);
        return new sun.misc.BASE64Encoder().encode(enc);

    }

    //Method Decrypt Password
    public static String decrypt(String input) throws Exception {
        KeySpec keySpec = new PBEKeySpec(PASSKEY.toCharArray(), salt, count);

        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(input);
        Cipher dcipher = Cipher.getInstance(key.getAlgorithm());
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt,count);
        dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        byte[] utf8 = dcipher.doFinal(dec);
        return new String(utf8,"UTF8");
    }



    public static String getProperty(String prop) {
        if(p == null) {
            return "";
        }

        return p.getProperty(prop);
    }

    public static void setProperty(String prop, String value) {
        if(p == null) {
            p = new Properties();
        }
        p.setProperty(prop, value);
    }

    public static boolean loadConfigurations() {
        p = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(CONFIG_FILENAME);
            p.load(fis);
            p.setProperty(CONFIG_SERVERPASSWORD, decrypt(p.getProperty(CONFIG_SERVERPASSWORD)));
            fis.close();
        } catch (Exception ex) {
            return false;
        }

        return true;
    }

    public static void saveConfiguration() {
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(CONFIG_FILENAME);
            String serverPass = p.getProperty(CONFIG_SERVERPASSWORD);
            p.setProperty(ConfigFile.CONFIG_SERVERPASSWORD, encrypt(serverPass));
            p.store(fos, "Configuration File");
            p.setProperty(ConfigFile.CONFIG_SERVERPASSWORD, serverPass);
            fos.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
    }
}
